﻿using NL;
using NL.Combat;
using NL.MiyaAssetBundle;
using NL.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

namespace XGame.UI
{
	/// <summary>
    /// 精力面板
    /// </summary>
    public class WinEnergy:Window
	{
        [Header("使用钻石按钮")]
        public ButtonEx usejewelBtn;

        [Header("看视频")]
        public ButtonEx watchvidoBtn;

        [Header("使用道具按钮")]
        public ButtonEx useItemBtn;

        [Header("不可使用道具按钮")]
        public ButtonEx unuseItemBtn;

        [Header("道具数量")]
        public Text ItemCount;

        /// <summary>
        /// 使用道具按钮回调
        /// 返回按钮是否需要置灰
        /// </summary>
        private Func<bool> UseItemFunc;

        protected override void Start()
        {
            base.Start();
            usejewelBtn?.onClick.AddListener(usejewelBtnOnClick);
            watchvidoBtn?.onClick.AddListener(usejewelBtnOnClick);
            useItemBtn?.onClick.AddListener(UsetItemBtn);

        }

        private void usejewelBtnOnClick()
        {
            WEBRES.LoadRES<GameObject>("Data/UI/messagebox.mab").Wait(w =>
            {
                if (w.LoadSuccess && w.LoadedObj != null)
                {
                    Box _win = (w.LoadedObj as GameObject)?.GetComponent<Box>();
                    if (_win != null)
                        NL.UI.UI.CreateWindow<Box>(_win).Show("", "Temporary not open!", UIButtonsModel.bmOk);
                }

            }, gameObject);
        }

        private void UsetItemBtn()
        {
            if(UseItemFunc != null)
            {
                bool btnEnabled = UseItemFunc.Invoke();
                UseItemBtnState(btnEnabled);
            }
        }

        /// <summary>
        /// 设置数量
        /// </summary>
        /// <param name="count">道具数量</param>
        /// <param name="btnEnabled">按钮是否置灰</param>
        /// <returns></returns>
        public WinEnergy SetItemCount(int count,bool btnEnabled)
        {
            ItemCount.text = string.Format("HAD  <color='#85F5FF'>{0}</color>  LEFT", count);
            UseItemBtnState( btnEnabled);
            return this;
        }

        /// <summary>
        /// 设置道具使用回调函数
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public WinEnergy SetUsetItemFunc(Func<bool> func)
        {
            UseItemFunc = func;
            return this;
        }


        private void UseItemBtnState(bool btnEnabled)
        {
            useItemBtn?.gameObject.SetActive(btnEnabled);
            unuseItemBtn?.gameObject.SetActive(!btnEnabled);
            unuseItemBtn.enabled = btnEnabled;
        }

    }
}
